<?php
include_once('../class/DBHelper.class.php');

class HomeModel extends DBHelper {

    public function insert($params) {
        if (count($params) < 1) {
            return false;
        }

        $holder = $this->holderInsert($params);
        $sql = "INSERT INTO `t_tuvi` (`ages`, `contents`, `years`, `image`) VALUES ($holder)";
        $result = $this->execSQL($sql, $params, true);
        return $result;
    }

    public function listData($start, $limit, $strSeach = '') {
        $sql = "select id, ages from `t_tuvi`";
        $params = array();
        $where = "";
        if ($strSeach != '') {
            $where = " where ages like ?";
            $params = array('s', '%'. $strSeach .'%');
        }

        $sql .= $where." order by id desc limit ".$start ."," .$limit;
        $list = $this->execSQL($sql, $params, false);

        return $list;
    }

    public function countData($strSeach = '') {
        $params = array();
        $where = "";
        if ($strSeach != '') {
            $where = " where ages like ?";
            $params = array('s', '%'. $strSeach .'%');
        }

        $sqlCount = "SELECT count(id) as count FROM `t_tuvi` " . $where;
        $resultTotal = $this->execSQL($sqlCount, $params, false);
        $total = $resultTotal[0]['count'];

        return $total;
    }

    public function getTuviById($id) {
        if (!is_numeric($id)) {
            return null;
        }
        $sql = "select * from `t_tuvi` where id = ?";
        $result = $this->execSQL($sql, array('i', $id), false);
        return $result;
    }

    public function delete($id) {
        if (!is_numeric($id)) {
            return null;
        }
        $sql = "delete from `t_tuvi` where id = ?";
        $result = $this->execSQL($sql, array('i', $id), true);
        return $result;
    }

    public function editTuvi($params) {
        if (count($params) < 1) {
            return false;
        }

        $sql = "UPDATE `t_tuvi` SET `ages` = ? , `contents` = ?, `years` =?, `image`=? WHERE id = ?";

        $result = $this->execSQL($sql, $params, true);
        return $result;
    }

}
?>